package com.swcw.platform.wlwLog.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.swcw.platform.external.constants.Pagination;
import com.swcw.platform.utils.BaseRquestAndResponse;
import com.swcw.platform.wlwLog.bean.WlwLog;
import com.swcw.platform.wlwLog.service.IWlwLogService;

/**
 * 工单表管理
 * @see: 
 * @Company:江苏鸿信系统集成有限公司微信开发组 
 * @author 郑建超
 * @Time 2016年12月22日
 * @version 1.0v
 */
@RestController
@RequestMapping("/jsp/wlwLog")
public class WlwLogController extends BaseRquestAndResponse {

	private static Logger logger = Logger.getLogger(WlwLogController.class);

	@Autowired
	private IWlwLogService wlwLogService;
	
	
	/**
	 * 日志列表
	 * 
	 * @author 郑建超
	 * @Time 2017年3月10日
	 * @version 1.0v
	 * @throws ParseException 
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("listLog")
	public void listLog() throws ParseException{
		logger.info("===============日志列表===============");
		
		String startTime = getRequest().getParameter("startTime");
		String endTime = getRequest().getParameter("endTime");
		String userName = getRequest().getParameter("userName");
		String logType = getRequest().getParameter("logType");
		Map<String, Object> paraMap = new HashMap<String, Object>();
		SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if(StringUtils.isNotEmpty(userName)){
			paraMap.put("userName", "%"+userName+"%");
		}
		if(StringUtils.isNotEmpty(logType)){
			paraMap.put("logType", logType);
		}
		if(StringUtils.isNotBlank(startTime)){
			paraMap.put("startTime", sf1.parse(startTime));
			paraMap.put("endTime", sf1.parse(endTime));
		}
		Map<String, Object> result = new HashMap<String, Object>();
		Pagination pagination = new Pagination(getRequest());
		pagination.setParaMap(paraMap);
		pagination = wlwLogService.selectFindByProblems(pagination);
		List<WlwLog> wlwLogs = (List<WlwLog>) pagination.getList();
		if(wlwLogs == null || wlwLogs.size() == 0){
			result.put("result", "M01");
			returnString(result);
			return;
		}
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<Map<String,Object>> results = new ArrayList<Map<String,Object>>();
		for (WlwLog wlwLog : wlwLogs) {
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("id", wlwLog.getId());
			map.put("logs", wlwLog.getLogs());
			map.put("dateTime", sf.format(wlwLog.getDateTime()));
			map.put("logType", wlwLog.getLogType());
			map.put("userName", wlwLog.getUserName());
			results.add(map);
		}
		result.put("result", "M01");
		result.put("items", results);
		result.put("totalCount", pagination.getTotalCount());
		returnString(result);
	}
}
